﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using quanlycd3lop.GUI;
using quanlycd3lop.Data;

namespace quanlycd3lop.BL
{
    class cl_ShopCD
    {
        AccessData con;
        Form frm;
        DataTable tb;
        public cl_ShopCD(frm_Shop f)
        {
            con = new AccessData();
            frm = f;
        }
        public void refesh()
        {
            ((DataGridView)frm.Controls["DataGridView2"]).DataSource = tb;
            ((TextBox)frm.Controls["txt_MaCD"]).DataBindings.Clear();
            ((ComboBox)frm.Controls["cbx_MaDM"]).DataBindings.Clear();
            ((TextBox)frm.Controls["txt_TenLoai"]).DataBindings.Clear();
            ((TextBox)frm.Controls["txt_DonGiaThue"]).DataBindings.Clear();

            ((TextBox)frm.Controls["txt_MaCD"]).DataBindings.Add("Text", tb, "MaCD");
            ((ComboBox)frm.Controls["cbx_MaDM"]).DataBindings.Add("Text", tb, "MaDM");
            ((TextBox)frm.Controls["txt_TenLoai"]).DataBindings.Add("Text", tb, "Tenloai");
            ((TextBox)frm.Controls["txt_DonGiaThue"]).DataBindings.Add("Text", tb, "Dongiathue");
            
        }
        public void hienthi(string MaCD)
        {
            con.Open();
            SqlParameter[] pr = new SqlParameter[1];
            if (MaCD != null)
                pr[0] = new SqlParameter("@MaCD", MaCD);
            else
                pr[0] = new SqlParameter("@MaCD", DBNull.Value);
            tb = con.Excute_ps("ps_LayShopCD",pr);
            con.Close();
            refesh();
        }
        public void themmoi()
        {
            string str_MaCD, str_MaDM, str_Tenloai, str_Dongiathue;
            str_MaCD = ((TextBox)frm.Controls["txt_MaCD"]).Text;
            str_MaDM = ((ComboBox)frm.Controls["cbx_MaDM"]).Text;
            str_Tenloai=((TextBox)frm.Controls["txt_TenLoai"]).Text;
            str_Dongiathue=((TextBox)frm.Controls["txt_DonGiaThue"]).Text;

            SqlParameter[] pr = { new SqlParameter("@MaCD",str_MaCD),
                                  new SqlParameter("@MaDM",str_MaDM),
                                  new SqlParameter("@Tenloai",str_Tenloai),
                                  new SqlParameter("@Dongiathue",str_Dongiathue)
                                };
            con.Open();
            tb = con.Excute_ps("ps_themshopcd",pr);
            con.Close();
            refesh();
        }
        public void xoa()
        {
            string str_MaCD;
            str_MaCD = ((TextBox)frm.Controls["txt_MaCD"]).Text;
            SqlParameter[] pr = { new SqlParameter("@MaCD",str_MaCD)
                                };
            con.Open();
            tb = con.Excute_ps("ps_xoashopcd", pr);
            con.Close();
            refesh();
        }
        public void sua()
        {
            string str_MaCD,str_Tenloai, str_Dongiathue;
            str_MaCD = ((TextBox)frm.Controls["txt_MaCD"]).Text;
            str_Tenloai = ((TextBox)frm.Controls["txt_TenLoai"]).Text;
            str_Dongiathue = ((TextBox)frm.Controls["txt_DonGiaThue"]).Text;

            SqlParameter[] pr = { new SqlParameter("@MaCD",str_MaCD),
                                  new SqlParameter("@Tenloai",str_Tenloai),
                                  new SqlParameter("@Dongiathue",str_Dongiathue)
                                };
            con.Open();
            tb = con.Excute_ps("ps_suashopcd", pr);
            con.Close();
            refesh();
        }
        public void set_combobox()
           {
                con.Open();
                SqlParameter[] pr = new SqlParameter[1];
                pr[0] = new SqlParameter("@MaDM",DBNull.Value);
                DataTable db = con.Excute_ps("ps_laydanhmuc", pr);
                ComboBox cb=((ComboBox)frm.Controls["cbx_MaDM"]);
                cb.DataSource = db;
                cb.DisplayMember = "MaDM";
                cb.ValueMember = "MaDM";
           }
    }
}
